﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Customer_Profile.Admin;
using KVB.Common;

namespace Customer_Profile
{
    public partial class menu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.Expires = 0;
            HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            HttpContext.Current.Response.AddHeader("pragma", "no-cache");
            HttpContext.Current.Response.AddHeader("cache-control", "private");
            HttpContext.Current.Response.CacheControl = "no-cache";
            Comm.CheckSession(this.Page, "/login.aspx", "usermodel");
            string userno = "";
            if (Session["usermodel"] != null)
            {
                userno = ((UserInfoEntity)Session["usermodel"]).UserId;
            }
            else
            {
                Log.WriteDefaultLog("menu Page_Load","Session is null");
                return;
            }
            string strUserID = userno;
            int i = 0;
            string Lvl1OrderID = "";
            string Lvl2OrderID = "";
            int oldLvl = 0;
            int LvlOld = 0;


            MenuInfoEntity objMenuEntity = new MenuInfoEntity();
            Response.Write("<?xml version='1.0' encoding='utf-8' ?>");
            Response.Write("<node label='root'>");

            DataSet objDs = MenuDao.getMenuRightList(strUserID);

            if (objDs != null)
            {
                for (i = 0; i < objDs.Tables[0].Rows.Count; i++)
                {
                    objMenuEntity.Lvl = Convert.ToInt32(objDs.Tables[0].Rows[i]["LVL"].ToString());
                    objMenuEntity.MenuName = objDs.Tables[0].Rows[i]["MENU_NAME"].ToString();
                    objMenuEntity.MenuId = objDs.Tables[0].Rows[i]["MENU_ID"].ToString();
                    objMenuEntity.ParentId = Convert.ToInt32(objDs.Tables[0].Rows[i]["PARENT_MENU_ID"].ToString());
                    if (objDs.Tables[0].Rows[i]["URL"].ToString().Equals(""))
                    {
                        objMenuEntity.Url = "";
                    }
                    else
                    {
                        objMenuEntity.Url = objDs.Tables[0].Rows[i]["URL"].ToString();
                    }
                    objMenuEntity.Orderid = objDs.Tables[0].Rows[i]["ORDER_ID"].ToString();
                    objMenuEntity.OpenMode = objDs.Tables[0].Rows[i]["ACTION_MODE"].ToString();

                    if (LvlOld == 0)
                    {
                        LvlOld = objMenuEntity.Lvl;
                    }
                    if (oldLvl == 0)
                    {
                        oldLvl = objMenuEntity.Lvl;
                    }

                    if (objMenuEntity.Lvl == 1)
                    {
                        if (Lvl1OrderID.Equals(""))
                        {
                            Lvl1OrderID = objMenuEntity.Orderid;
                            Response.Write("<node label='" + objMenuEntity.MenuName + "'>");
                        }
                        else
                        {
                            if (!Lvl1OrderID.Equals(objMenuEntity.Orderid))
                            {
                                Response.Write("</node>");
                                Response.Write("<node label='" + objMenuEntity.MenuName + "'>");

                            }
                            Lvl1OrderID = objMenuEntity.Orderid;
                        }
                    }

                    if (objMenuEntity.Lvl == 2)
                    {
                        if (Lvl2OrderID.Equals(""))
                        {
                            Lvl2OrderID = objMenuEntity.Orderid;
                            if (!objMenuEntity.Url.Equals(""))
                            {
                                Response.Write("<node label='" + objMenuEntity.MenuName + "' url='" + objMenuEntity.Url + "' isBranch='true' target='" + objMenuEntity.OpenMode + "'>");
                            }
                            else
                            {
                                Response.Write("<node label='" + objMenuEntity.MenuName + "'>");
                            }
                        }
                        else
                        {
                            if (!Lvl2OrderID.Equals(objMenuEntity.Orderid))
                            {
                                if (oldLvl != 1)
                                {

                                    Response.Write("</node>");
                                }
                                if (!objMenuEntity.Url.Equals(""))
                                {
                                    Response.Write("<node label='" + objMenuEntity.MenuName + "' url='" + objMenuEntity.Url + "' isBranch='true' target='" + objMenuEntity.OpenMode + "'>");
                                }
                                else
                                {
                                    Response.Write("<node label='" + objMenuEntity.MenuName + "'>");
                                }

                            }
                            Lvl2OrderID = objMenuEntity.Orderid;
                        }
                    }

                    if (objMenuEntity.Lvl == 3)
                    {
                        Response.Write("<node label='" + objMenuEntity.MenuName + "' url='" + objMenuEntity.Url + "' isBranch='true' target='" + objMenuEntity.OpenMode + "'>");
                        Response.Write("</node>");

                        LvlOld = objMenuEntity.Lvl;
                    }
                    oldLvl = objMenuEntity.Lvl;

                    if (objDs.Tables[0].Rows.Count != 1)
                    {
                        if (i == objDs.Tables[0].Rows.Count - 1)
                        {
                            Response.Write("</node>");
                        }
                        else
                        {
                            if (Convert.ToInt32(objDs.Tables[0].Rows[i + 1]["PARENT_MENU_ID"].ToString()) == 0)
                            {
                                Response.Write("</node>");
                            }
                        }
                    }

                }
            }

            if (objDs != null)
            {
                Response.Write("</node>");
            }
            Response.Write("</node>");
        }
    }
}
